<?php

/**
 * Created by SNWAY.
 * User: w
 * Date: 2017/5/26
 * Time: 12:06
 */
namespace app\index;

use think\Model;
use think\View;
use think\Request;
use think\Session;
use think\Db;
use think\Config;
use think\Loader;
use think\Exception;
use think\exception\HttpException;

class Controller
{
    use \traits\controller\Jump;
    /**
     * @var View 视图类实例
     */
    protected $view;
    /**
     * @var Request Request实例
     */
    protected $request;

    /**
     * @var controller
     */
    protected $controller;
    /**
     * @var action
     */
    protected $action;

    public function __construct()
    {        
        if (null === $this->request) {
            $this->request = Request::instance();
        }
        $this->controller = $this->request->controller();
        $this->action = $this->request->action();
        $this->authorizedAccess();
        
        if (null === $this->view) {
            $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
        }


        // 用户ID
        defined('UID') or define('UID', Session::get(Config::get('user.user_auth_key')));

        // 前置方法
        $beforeAction = "before" . $this->action;
        if (method_exists($this, $beforeAction)) {
            $this->$beforeAction();
        }
    }

    protected function  authorizedAccess()
    {
        if (!session(Config::get('user.user_auth_key')) && !session(Config::get('user.user_auth_value'))) {
            $this->redirect('Login/index');die();
        } else {
            //已登录
        }
        $employeeId = session(Config::get('user.user_auth_key'));

        //是否超人
        if ($employeeId === Config::get('user.guest_auth_id')) {
            //go
        } else {
            $cpd = Db::name("auth_assign")
                ->where('employeeId', $employeeId)
                ->value('group_concat(childPageId)');
            $controllerName = $this->controller;
            $actionName = $this->action;
            $param = $this->request->param();
            $pageAction = $controllerName . '/' . $actionName;
            $skipAction = Config::get('user.free_auth_action');
            //是否是ignore页面
            if (!in_array($pageAction, $skipAction, false)) {
                if (empty($cpd)) {
                    $this->goOut();
                }
                $pq = Db::name("child_page c")
                    ->where("FIND_IN_SET(c.childPageId,'".$cpd."')")
                    ->where('c.pageAction', '=', $pageAction)
                    ->field('c.pageQuery')
                    ->select();
                //是否存在权限页面
                if (empty($pq)) {
                    $this->goOut();
                } else {//检查附加参数
                    foreach ($pq as $k=>$v){
                        if(!empty($v["pageQuery"])){
                            $paramValue=json_decode($v["pageQuery"]);
                            if(empty($paramValue)){
                                $this->goOut();
                            }
                            else{
                                foreach ($paramValue as $key => $item) {
                                    if ($param[$key] !== $item) {
                                        $this->goOut();
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                //go
            }
        }
    }

    protected function goOut()
    {
        if (request()->isAjax()) {
            $arr['code'] = 1;
            $arr['msg'] = "没有操作权限";
            echo json_encode($arr);
        }else {
            echo("没有操作权限!");
        }
        die();
    }
}